class SearchParams_ctrip:
    def __init__(self,
                 countryId=1,
                 destination="北京",
                 provinceId=0,
                 checkin="2025/02/27",
                 checkout="2025/03/01",
                 optionId=1,
                 optionType="City",
                 directSearch=1,
                 detailDestination="北京大学",
                 crn=1,
                 adult=2,
                 children=0,
                 searchBoxArg="t",
                 travelPurpose=0,
                 ctm_ref="ix_sb_dl",
                 domestic=1,
                 highPrice=-1,
                 barCurr="CNY",
                 bedType=1,
                 sort=9):
        self.countryId = countryId
        self.city = self.get_cityNum(destination)
        self.provinceId = provinceId
        self.checkin = checkin
        self.checkout = checkout
        self.optionId = self.city
        self.optionType = optionType
        self.directSearch = directSearch
        self.optionName = detailDestination
        self.display = detailDestination
        self.crn = crn
        self.adult = adult
        self.children = children
        self.searchBoxArg = searchBoxArg
        self.travelPurpose = travelPurpose
        self.ctm_ref = ctm_ref
        self.domestic = domestic
        self.highPrice=highPrice
        self.barCurr = barCurr
        self.bedType = bedType
        self.sort = sort

    def to_query(self):
        from urllib.parse import urlencode
        return urlencode(self.__dict__)
    
    def get_cityNum(self, destination):
        mapping = {
            "北京": 1,
            "上海": 2,
            "广州": 3,
            "深圳": 4,
        }
        return mapping.get(destination)


class SearchParams_fliggy:
    def __init__(self,
                _input_charset="utf8",
                _output_charset="utf8",
                tid="undefined",
                U="undefined",
                searchBy="",
                market=0,
                previousChannel="undefined",
                destination="北京",
                checkin="2025/02/27",
                checkout="2025/03/01",
                detailDestination="北京大学",
                ):
        self._input_charset = _input_charset
        self._output_charset = _output_charset
        self.tid = tid
        self.U = U
        self.searchBy = searchBy
        self.market = market
        self.previousChannel = previousChannel
        self.cityName = destination
        self.city= self.get_cityNum(destination)
        self.__dict__["_fmd.h._0.r"] = ""
        self.checkIn = self.format_date(checkin)
        self.checkOut = self.format_date(checkout)
        self.keywords = detailDestination

    def to_query(self):
        from urllib.parse import urlencode
        return urlencode(self.__dict__)
    
    def get_cityNum(self, destination):
        import pandas as pd
        df = pd.read_csv('other/citycode.csv')
        city_code = df.loc[df['中文名'].str.contains(destination) , 'adcode']
        if not city_code.empty:
            return city_code.values[0]
        else:
            return None
        
    def format_date(self, date_str):
        # 将日期格式从 YYYY/MM/DD 转换为 YYYY-MM-DD
        return date_str.replace('/', '-')


class SearchParams_ly:
    def __init__(self,
                destination="北京",
                checkin="2025/02/27",
                checkout="2025/03/01",
                detailDestination="北京大学",
                filterList="1040_36,8888_1",  # 默认按照推荐排序
                ):
        self.city = self.get_cityNum(destination)
        self.inDate = self.format_date(checkin)
        self.outDate = self.format_date(checkout)
        self.keywords = detailDestination
        self.filterList = filterList

    def to_query(self):
        from urllib.parse import urlencode
        return urlencode(self.__dict__)
    
    def get_cityNum(self, destination):
        # 同程旅行的城市编码映射
        mapping = {
            "北京": 53,
            "上海": 321,
            "广州": 446,
            "深圳": 447,
            # 可以继续添加其他城市
        }
        return mapping.get(destination)
        
    def format_date(self, date_str):
        # 将日期格式从 YYYY/MM/DD 转换为 YYYY-MM-DD
        return date_str.replace('/', '-')

if __name__ == "__main__":
    params = SearchParams_ly(checkin="2025/03/08", checkout="2025/03/09",destination="北京",detailDestination="北京大学")

    # base_url = "https://hotels.ctrip.com/hotels/list"
    # base_url = "https://hotel.fliggy.com/hotel_list3.htm"
    base_url ="https://www.ly.com/hotel/hotellist"
    full_url = f"{base_url}?{params.to_query()}"
    print(full_url)